package com.test.template.publish.config.fac;

import com.test.template.publish.config.XmlIndexConfig;
import com.test.template.publish.config.manager.VelocityConfigManager;
import lombok.Data;

import java.util.concurrent.ConcurrentHashMap;


@Data
public class XmlIndexConfigFactory implements VelocityTemplateConfigFactory<XmlIndexConfig> {

    ConcurrentHashMap<String, XmlIndexConfig> configHashMap = new ConcurrentHashMap<>();

    // **单例定义
    // 私有静态变量保存唯一实例
    private static volatile XmlIndexConfigFactory instance = null;

    // **单例定义
    // 公共静态方法提供全局访问点
    public static XmlIndexConfigFactory getInstance() {
        if (instance == null) {
            synchronized (XmlIndexConfigFactory.class) {
                if (instance == null) {
                    instance = new XmlIndexConfigFactory();
                    init();
                }
            }
        }
        return instance;
    }

    /**
     * description 初始化函数，后续修改为配置文件读取方式执行
     *
     * @author zken
     * @CreateDate 2024/12/6 20:46:59
     */
    private static void init() {
        XmlIndexConfig contributeConfig = buildBasicConfig("contribute");
        getInstance().addConfig("contribute", contributeConfig);
    }

    /**
     * 创建
     *
     * @return
     */
    public static XmlIndexConfig buildBasicConfig(String tableName) {
        XmlIndexConfig config = new XmlIndexConfig();
        config.setTableName(tableName);
        config.innerGenerate();
        return config;
    }


    // 私有构造函数防止实例化
    private XmlIndexConfigFactory() {

    }

    @Override
    public void addConfig(String key, XmlIndexConfig config) {
        configHashMap.put(key, config);
    }

    @Override
    public XmlIndexConfig getConfig(String key) {
        return configHashMap.get(key);
    }

    @Override
    public void addConfig(VelocityConfigManager manager) {
        XmlIndexConfig config = buildBasicConfig(manager.getTableName());
        getInstance().addConfig(manager.getTableName(), config);
    }


}
